import { baseUrl } from './env'
import axios from 'axios'
// import {Message} from 'element-ui'

axios.defaults.baseURL = baseUrl;
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.interceptors.response.use((res) => {
  if (res.status >= 200 && res.status < 300) {
    return Promise.resolve(res);
  }
  return Promise.reject({message: '请求异常，请重试', err: res});
}, (e) => {
  // 网络异常
  return Promise.reject({message: '网络异常，请刷新重试', err: e});
});

export default async(url = '', data = {}, type = 'GET', queryData = {}) => {
    type = type.toUpperCase();
    url = url;
    if (type == 'GET') {
        queryData = data;
    }
    try {
        const response = await axios({
            method: type,
            url: url,
            params: queryData,
            data: data
        });
        return response.data
    } catch(error){
        let resp = error.err.response?error.err.response.data:{code:"8888",message:error.message};
        // Message.error(resp.message+"（"+resp.code+"）")
        return resp;
    }

}
